package com.yanchangapp.ui.activity.purchase.contract;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.yanchangapp.R;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.ui.activity.purchase.POrderListActivity;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.util.time.TimeUtil;
import com.zxl.baselib.util.ui.ResHelper;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.util.window.KeyboardHelper;

import java.util.Calendar;
import java.util.Date;
import butterknife.BindView;

/**
 * 合同筛选页面
 */
public class ContractFilterActivity extends BaseActivity {
    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.tvTitleStartTime)
    TextView tvTitleStartTime;
    @BindView(R.id.ivStartArrow)
    ImageView ivStartArrow;
    @BindView(R.id.tvStartTime)
    TextView tvStartTime;
    @BindView(R.id.rlStartTime)
    RelativeLayout rlStartTime;
    @BindView(R.id.tvTitleEndTime)
    TextView tvTitleEndTime;
    @BindView(R.id.ivEndArrow)
    ImageView ivEndArrow;
    @BindView(R.id.tvEndTime)
    TextView tvEndTime;
    @BindView(R.id.rlEndTime)
    RelativeLayout rlEndTime;
    @BindView(R.id.etKeywords)
    EditText etKeywords;
    @BindView(R.id.tvReset)
    TextView tvReset;
    @BindView(R.id.tvEnsure)
    TextView tvEnsure;

    private static final String KEYWORDS = "keyword";
    private static final String START_TIME = "startTime";
    private static final String END_TIME = "endTime";

    private String mKeywords;
    private String mStartTime;
    private String mEndTime;

    private boolean isShowStartTime;
    private TimePickerView pvCustomTime;

    public static void show(Activity activity, String keywords, String startTime, String endTime) {
        Intent intent = new Intent(activity, ContractFilterActivity.class);
        intent.putExtra(KEYWORDS, keywords);
        intent.putExtra(START_TIME, startTime);
        intent.putExtra(END_TIME, endTime);
        activity.startActivityForResult(intent, AppConstant.RECODE_CONTRACT_FILTER_ATY);
    }

    @Override
    protected void init() {
        if (getIntent() == null) {
            finish();
        } else {
            mKeywords = getIntent().getStringExtra(KEYWORDS);
            mStartTime = getIntent().getStringExtra(START_TIME);
            mEndTime = getIntent().getStringExtra(END_TIME);
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_contract_filter;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvToolbarTitle.setText("筛选");
        initCustomTimePicker();
        if (!TextUtils.isEmpty(mKeywords)){
            etKeywords.setText(mKeywords);
        }

        if (!TextUtils.isEmpty(mStartTime)){
            tvStartTime.setText(TimeUtil.getDayTime(mStartTime));
        }

        if (!TextUtils.isEmpty(mEndTime)){
            tvEndTime.setText(TimeUtil.getDayTime(mEndTime));
        }
    }

    private Calendar getCalendar(boolean isStart){
        String tvTime;
        if (isStart){
            tvTime = mStartTime;
        }else {
            tvTime = mEndTime;
        }
        // 判断时间是否为空...
        Calendar instance  = Calendar.getInstance();
        if (TextUtils.isEmpty(tvTime)){
            // 如果是空的话 默认显示的是当前的时间...
            instance.set(Calendar.HOUR_OF_DAY,0);
            instance.set(Calendar.MINUTE,0);
            instance.set(Calendar.SECOND,0);
        }else {
            Date date = TimeUtil.string2Date(tvTime, "yyyy-MM-dd HH:mm:ss");
            instance.setTime(date);
        }
        return  instance;
    }


    private void reBackInfo(String keywords,String startTime,String endTime){
        Intent intent = new Intent();
        intent.putExtra(POrderListActivity.KEYWORDS,keywords);
        intent.putExtra(POrderListActivity.START_TIME,startTime);
        intent.putExtra(POrderListActivity.END_TIME,endTime);
        setResult(RESULT_OK,intent);
        finish();
    }

    private void initCustomTimePicker() {
        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        Calendar nowCalendar = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        startDate.set(2016, 1, 1);
        Calendar endDate = Calendar.getInstance();
        endDate.set(nowCalendar.get(Calendar.YEAR)+1, 1, 1);
        //时间选择器 ，自定义布局
        pvCustomTime = new TimePickerBuilder(this, (date, v) -> {//选中事件回调
            String tvDay = TimeUtil.date2String(date,"yyyy-MM-dd");

            if (isShowStartTime){
                mStartTime = tvDay+" 00:00:00";
                tvStartTime.setText(tvDay);
            }else {
                mEndTime = tvDay+" 23:59:59";
                tvEndTime.setText(tvDay);
            }
        })
                /*.setType(TimePickerView.Type.ALL)//default is all
                .setCancelText("Cancel")
                .setSubmitText("Sure")
                .setContentTextSize(18)
                .setTitleSize(20)
                .setTitleText("Title")
                .setTitleColor(Color.BLACK)
               /*.setDividerColor(Color.WHITE)//设置分割线的颜色
                .setTextColorCenter(Color.LTGRAY)//设置选中项的颜色
                .setLineSpacingMultiplier(1.6f)//设置两横线之间的间隔倍数
                .setTitleBgColor(Color.DKGRAY)//标题背景颜色 Night mode
                .setBgColor(Color.BLACK)//滚轮背景颜色 Night mode
                .setSubmitColor(Color.WHITE)
                .setCancelColor(Color.WHITE)*/
                /*.animGravity(Gravity.RIGHT)// default is center*/
                .setTitleColor(ResHelper.getColor(R.color.colorPrimary))
                .setRangDate(startDate, endDate)
                .setContentTextSize(18)
                .setType(new boolean[]{true, true, true, false, false, false})
                .setLabel("年", "月", "日", "时", "分", "秒")
                .setLineSpacingMultiplier(1.6f)
                .setTextXOffset(0, 0, 0, 40, 0, -40)
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDecorView((ViewGroup) getWindow().getDecorView().findViewById(R.id.rlFilter))
                .build();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        ivToolbarNavigation.setOnClickListener(v -> finish());

        tvReset.setOnClickListener(v -> reBackInfo(null,null,null));

        tvEnsure.setOnClickListener(v -> {
            // 设置数据 ==> 返回数据
            if (TimeUtil.isTimeOk(mStartTime,mEndTime)){
                mKeywords = etKeywords.getText().toString();
                reBackInfo(mKeywords,mStartTime,mEndTime);
            }else {
                UIUtils.showToast("终止时间不能早于起始时间");
            }

        });

        rlStartTime.setOnClickListener(v -> {
            isShowStartTime = true;
            KeyboardHelper.hideKeyboard(tvToolbarTitle);
            pvCustomTime.setDate(getCalendar(true));
            pvCustomTime.show();
        });

        rlEndTime.setOnClickListener(v -> {
            isShowStartTime = false;
            KeyboardHelper.hideKeyboard(tvToolbarTitle);
            pvCustomTime.setDate(getCalendar(false));
            pvCustomTime.show();
        });
    }

}
